<!--
 * @Description: 任务搜索组件
-->
<template>
    <div class="task-search">
      <el-form :model="searchForm" :inline="true" class="search-form">
        <el-form-item label="任务标题">
          <el-input
            v-model="searchForm.title"
            placeholder="请输入任务标题"
            clearable
            style="width: 200px"
          />
        </el-form-item>
        
        <el-form-item label="任务状态">
          <el-select
            v-model="searchForm.status"
            placeholder="请选择任务状态"
            clearable
            style="width: 150px"
          >
            <el-option label="待处理" value="pending" />
            <el-option label="进行中" value="in-progress" />
            <el-option label="已完成" value="completed" />
            <el-option label="已取消" value="cancelled" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="分配人员">
          <el-select
            v-model="searchForm.assignedTo"
            placeholder="请选择分配人员"
            clearable
            style="width: 150px"
          >
            <el-option 
              v-for="person in personnelOptions" 
              :key="person.name"
              :label="person.name" 
              :value="person.name" 
            />
          </el-select>
        </el-form-item>
        
        <el-form-item label="所属部门">
          <el-select
            v-model="searchForm.department"
            placeholder="请选择部门"
            clearable
            style="width: 180px"
          >
            <el-option 
              v-for="dept in departmentOptions" 
              :key="dept"
              :label="dept" 
              :value="dept" 
            />
          </el-select>
        </el-form-item>
        
        <el-form-item label="优先级">
          <el-select
            v-model="searchForm.priority"
            placeholder="请选择优先级"
            clearable
            style="width: 120px"
          >
            <el-option label="紧急" value="urgent" />
            <el-option label="高" value="high" />
            <el-option label="中" value="medium" />
            <el-option label="低" value="low" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="searchForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            style="width: 240px"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            查询
          </el-button>
          <el-button @click="handleReset">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </template>
  
  <script setup lang="ts">
  import { ref, computed, watch } from 'vue';
  import { Search, Refresh } from '@element-plus/icons-vue';
  
  // 接口定义
  interface PersonnelInfo {
    index: number;
    personnelCategory: string;
    department: string;
    name: string;
    gender: string;
    education: string;
    degree: string;
    graduatedFrom: string;
    personnelNature: string;
    workContent: string;
    workLocation: string;
    entryDate: string;
    contractStartDate: string;
    contractEndDate: string;
  }
  
  interface SearchParams {
    title?: string;
    status?: string;
    assignedTo?: string;
    department?: string;
    priority?: string;
    dateRange?: [string, string];
  }
  
  // Props 和 Emits
  const props = defineProps<{
    personnelList?: PersonnelInfo[];
  }>();
  
  const emit = defineEmits<{
    search: [params: SearchParams];
    reset: [];
  }>();
  
  // 搜索表单数据
  const searchForm = ref<SearchParams>({
    title: '',
    status: '',
    assignedTo: '',
    department: '',
    priority: '',
    dateRange: undefined
  });
  
  // 人员选项
  const personnelOptions = computed(() => {
    return props.personnelList || [];
  });
  
  // 部门选项
  const departmentOptions = computed(() => {
    const departments = new Set<string>();
    if (props.personnelList) {
      props.personnelList.forEach(person => {
        departments.add(person.department);
      });
    }
    // 添加一些常见部门
    departments.add('减灾所工程部');
    departments.add('减灾所抗旱部');
    departments.add('减灾所洪管部');
    departments.add('减灾所技术部');
    departments.add('中心综合办公室');
    
    return Array.from(departments).sort();
  });
  
  // 搜索处理
  const handleSearch = () => {
    const params: SearchParams = {};
    
    if (searchForm.value.title?.trim()) {
      params.title = searchForm.value.title.trim();
    }
    if (searchForm.value.status) {
      params.status = searchForm.value.status;
    }
    if (searchForm.value.assignedTo) {
      params.assignedTo = searchForm.value.assignedTo;
    }
    if (searchForm.value.department) {
      params.department = searchForm.value.department;
    }
    if (searchForm.value.priority) {
      params.priority = searchForm.value.priority;
    }
    if (searchForm.value.dateRange && searchForm.value.dateRange.length === 2) {
      params.dateRange = searchForm.value.dateRange;
    }
    
    emit('search', params);
  };
  
  // 重置处理
  const handleReset = () => {
    searchForm.value = {
      title: '',
      status: '',
      assignedTo: '',
      department: '',
      priority: '',
      dateRange: undefined
    };
    emit('reset');
  };
  
  // 监听表单变化，实时搜索
  watch(
    () => searchForm.value,
    () => {
      // 可以根据需要开启实时搜索
      // handleSearch();
    },
    { deep: true }
  );
  </script>
  
  <style lang="scss" scoped>
  .task-search {
    .search-form {
      .el-form-item {
        margin-bottom: 16px;
      }
    }
  }
  
  // 响应式布局
  @media (max-width: 1200px) {
    .search-form {
      .el-form-item {
        display: block;
        margin-right: 0;
      }
    }
  }
  </style>